package com.da.demo;

/**
 * @Description: TODO(表的属性信息)
 * @Author da
 * @Date 2022/2/28 下午 1:03
 */
public class ColumnInfo
{
    //    属性的名字
    private String columnName;
    //    属性的类型
    private String typeName;
    //    属性备注
    private String remarks;
    //    字段序号
    private Integer ordinalPosition;


    //    生成当前属性的java代码
    public String createJavaCode()
    {
        return String.join("\n",
                "\t/**",
                "\t * 属性备注: " + this.remarks,
                "\t */",
                "\tprivate " + this.toJavaType() + " " + this.toFieldName() + ";",
                "\n\tpublic " + this.toJavaType() + " " + this.toGetFieldName() + "() {",
                "\t\treturn this." + this.toFieldName() + ";",
                "\t}",
                "\n\tpublic void " + this.toSetFieldName() + "(" + this.toJavaType() + " " + this.toFieldName() + ") {",
                "\t\tthis." + this.toFieldName() + " = " + this.toFieldName() + ";",
                "\t}"

        );
    }

    //    获取属性名字
    private String toFieldName()
    {
        return StringUtil.tableNameToClassName(this.columnName).substring(0, 1).toLowerCase() + this.columnName.substring(1);
    }

    //    获取属性get方法
    private String toGetFieldName()
    {
        return "get" + StringUtil.tableNameToClassName(this.columnName);
    }

    //    获取属性set方法
    private String toSetFieldName()
    {
        return "set" + StringUtil.tableNameToClassName(this.columnName);
    }

    //    获取属性类型
    private String toJavaType()
    {
        switch (this.typeName)
        {
            case "INT":
            case "BIGINT":
            case "TINYINT":
                return "Long";
            case "VARCHAR":
            case "CHAR":
                return "String";
            case "DATETIME":
                return "Date";
            default:
                throw new IllegalArgumentException("未知数据库类型");
        }
    }

    public ColumnInfo()
    {
    }

    public ColumnInfo(String columnName, String typeName, String remarks, Integer ordinalPosition)
    {
        this.columnName = columnName;
        this.typeName = typeName;
        this.remarks = remarks;
        this.ordinalPosition = ordinalPosition;
    }

    public String getRemarks()
    {
        return remarks;
    }

    public void setRemarks(String remarks)
    {
        this.remarks = remarks;
    }

    public String getColumnName()
    {
        return columnName;
    }

    public void setColumnName(String columnName)
    {
        this.columnName = columnName;
    }

    public String getTypeName()
    {
        return typeName;
    }

    public void setTypeName(String typeName)
    {
        this.typeName = typeName;
    }

    public Integer getOrdinalPosition()
    {
        return ordinalPosition;
    }

    public void setOrdinalPosition(Integer ordinalPosition)
    {
        this.ordinalPosition = ordinalPosition;
    }

    @Override
    public String toString()
    {
        return "ColumnInfo{" +
                "columnName='" + columnName + '\'' +
                ", typeName='" + typeName + '\'' +
                ", remarks='" + remarks + '\'' +
                ", ordinalPosition=" + ordinalPosition +
                '}';
    }
}

